package com.project.config.http;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;

import java.io.IOException;
import java.net.URLDecoder;

@Slf4j
public class TrackLogClientHttpRequestInterceptor implements ClientHttpRequestInterceptor {
    @Override
    public ClientHttpResponse intercept(HttpRequest httpRequest, byte[] bytes,
                                        ClientHttpRequestExecution clientHttpRequestExecution) throws IOException {
        log.info("--------------------------------------------------------------------------------------------------------------------------------------------------");
        //打印请求日志
        log.info("Http Request : [{}] [{}] [{}] [{}]", httpRequest.getMethod(),
                URLDecoder.decode(httpRequest.getURI().toString(), "UTF-8"),
                new String(bytes, "UTF-8"), httpRequest.getHeaders());
        ClientHttpResponse httpResponse = clientHttpRequestExecution.execute(httpRequest, bytes);
        //打印响应日志
        log.info("Http Response : [{}] [{}] [{}]", httpResponse.getStatusCode(),
                httpResponse.getStatusText(), httpResponse.getHeaders());
        return httpResponse;
    }

}
